package Formularios;

import BD.Validacao;
import BD.produtos;
import BD.sabores;
import BD.sabores_Itens;
import BD.utilsPim;
import java.sql.Array;
import java.util.ArrayList;
import javax.swing.JOptionPane;

public class FormSabores extends javax.swing.JFrame {
    private boolean Incluindo;
    private boolean Incluindo_itens;
    private int ponteiro = 0;
    private int ponteiro_itens = 0;
    private ArrayList<sabores> listSabores = new ArrayList<sabores>();
    private Validacao vldc = new Validacao();
    private utilsPim utlp = new utilsPim();
    /**
     * Creates new form FormSabor
     */
    public FormSabores() {
        initComponents();
        TravaBtns(false, true, false,false);
        CarregaCombobox();
        setLocationRelativeTo(null);
    }
    private void CarregaCombobox() {
        produtos produto = new produtos();
        produto.setIngrediente(true);
        produto.pesp_ingredi = true;
        jcbxproduto.removeAllItems();
        ArrayList<produtos>  listProd = new ArrayList<produtos>();
        listProd = produto.getItens();
        
        for (int i = 0; i < listProd.size(); i++) {
            jcbxproduto.addItem(listProd.get(i).getDescricao());
        }
        jcbxproduto.setSelectedIndex(-1);
    }
    private void limpaCampo() {
        this.jtcodigo.setText("");
        this.jtDescricao.setText("");        
//        this.jtcbPesq.setText("");
    }
    private void limparCampoItens(){
        this.jtqtd.setText("");
        this.jcbxproduto.setSelectedIndex(-1);
        this.jtcodigo1.setText("");
    }
            
    private boolean Validacao(boolean Iten) {       

//        return (vldc.Valida(jcbxproduto.getSelectedItem(), "Produto"));
        if(Iten){
        String   s = "";
        if( jcbxproduto.getSelectedIndex()< 0)        
            s = "";
        else
            s =(String) jcbxproduto.getSelectedItem();
        
        if(jtqtd.getText().equals("")){
            JOptionPane.showMessageDialog(rootPane,"O campo quantidade precisa ser preenchido");
            return false;            
        }           
        
        return vldc.Valida(s, "Produto");
        }
        else
            return (vldc.Valida(jtDescricao.getText(), "Descrição Sabor"));            
//                && vldc.Valida(jtqtd.getText(), "Quantidade") && vldc.Valida(jtCusto.getText(), "Custo"));*/
    }
    

    private void TravaBtns(boolean incluindo, boolean navegando, boolean pesquisando,boolean incluindo_itens) {
        jtcodigo.setEditable(false);        
        jtDescricao.setEditable(incluindo);
        jpNavegacao.setVisible(!incluindo);        
        
        jcbxproduto.setEditable(Incluindo_itens);
        //jpnlAtrazCbxPro.setEnabled(Incluindo_itens);
        
        
            jbtncancelar1.setVisible((!incluindo)&& (incluindo_itens));
            jbtnGravar1.setVisible((!incluindo)&& (incluindo_itens));
            jbtnexcluir1.setVisible((!incluindo)&& (!incluindo_itens)&&(navegando));
            jbtnNovo1.setVisible((!incluindo)&& (!incluindo_itens)&&(navegando));
            jbtnAlterar1.setVisible((!incluindo)&& (!incluindo_itens)&&(navegando));
            jbtnPrior1.setVisible((!incluindo)&& (!incluindo_itens)&&(navegando));
            jbtnNext1.setVisible((!incluindo)&& (!incluindo_itens)&&(navegando));

        if (incluindo) {
            jbtncancelar.setVisible(true);
            jbtnGravar.setVisible(true);
            jpnlPesq.setVisible(false);
            jbtnexcluir.setVisible(false);
            jbtnNovo.setVisible(false);
            jbtnAlterar.setVisible(false); 
            jPnlItens.setVisible(false);            
        } else if (navegando) {
            jbtnAlterar.setVisible(true);
            jbtncancelar.setVisible(false);
            jbtnGravar.setVisible(false);
            jpnlPesq.setVisible(true);
            jbtnexcluir.setVisible(true);
            jbtnNovo.setVisible(true);
            jPnlItens.setVisible(true);            
        }else if(incluindo_itens){
            jbtncancelar.setVisible(false);
            jbtnGravar.setVisible(false);
            jpnlPesq.setVisible(false);
            jbtnexcluir.setVisible(false);
            jbtnNovo.setVisible(false);
            jbtnAlterar.setVisible(false); 

            jbtncancelar1.setVisible(true);
            jbtnGravar1.setVisible(true);
            jbtnexcluir1.setVisible(false);
            jbtnNovo1.setVisible(false);
            jbtnAlterar1.setVisible(false); 
            jbtnPrior1.setVisible(false); 
            jbtnNext1.setVisible(false); 
            
            jPnlItens.setVisible(true);            
        }else
        {
            jbtncancelar.setVisible(false);
            jbtnGravar.setVisible(false);
            jpnlPesq.setVisible(false);
            jbtnexcluir.setVisible(false);
            jbtnNovo.setVisible(false);
            jbtnAlterar.setVisible(false); 

            
        }    
            

        //jbtnPesquiza.enable(pTipo);
    }
    
    private void movPonteiro(boolean first, boolean prior, boolean next, boolean last) {
        if (listSabores.size() > 0) {
            if (first) {
                this.ponteiro = 0;
            } else if (prior) {
                this.ponteiro = this.ponteiro - 1;
                if (this.ponteiro < 0) {
                    this.ponteiro = 0;
                }
            } else if (next) {
                this.ponteiro = this.ponteiro + 1;
                if (this.ponteiro > (listSabores.size() - 1)) {
                    this.ponteiro = (listSabores.size() - 1);
                }
            } else if (last) {
                this.ponteiro = (listSabores.size() - 1);
            }
            
            this.jtcodigo.setText(Integer.toString(listSabores.get(this.ponteiro).getId_sabores()));
            this.jtDescricao.setText(listSabores.get(this.ponteiro).getDescricao());            
            
            this.jcbxproduto.removeAllItems();           
            if(listSabores.get(this.ponteiro).getListsabores_itens().size() > (0)){
                //System.out.println(listSabores.get(this.ponteiro).getListsabores_itens().size());
                this.jtcodigo1.setText(Integer.toString(listSabores.get(this.ponteiro).getListsabores_itens().get(0).getId_sabores_itens()));
                this.jcbxproduto.addItem(listSabores.get(this.ponteiro).getListsabores_itens().get(0).getDesc_produto());                    
                this.jtqtd.setText(Integer.toString(listSabores.get(this.ponteiro).getListsabores_itens().get(0).getQtd()));
                this.jcbxproduto.setSelectedIndex(0);                                  
            }else
                limparCampoItens();

        } else {
            limpaCampo();
        }
    }
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel7 = new javax.swing.JLabel();
        jpnlPesq = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jtPesq = new javax.swing.JTextField();
        jbtnPesquisa = new javax.swing.JButton();
        jpNavegacao = new javax.swing.JPanel();
        jbtnFirst = new javax.swing.JButton();
        jbtnPrior = new javax.swing.JButton();
        jbtnNext = new javax.swing.JButton();
        jbtnLast = new javax.swing.JButton();
        jSeparator3 = new javax.swing.JSeparator();
        jbtnexcluir = new javax.swing.JButton();
        jbtnGravar = new javax.swing.JButton();
        jbtncancelar = new javax.swing.JButton();
        jbtnNovo = new javax.swing.JButton();
        jbtnAlterar = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jtcodigo = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jtDescricao = new javax.swing.JTextField();
        jPnlItens = new javax.swing.JPanel();
        labelProdutos = new javax.swing.JLabel();
        jbtnNovo1 = new javax.swing.JButton();
        jbtnAlterar1 = new javax.swing.JButton();
        jbtnGravar1 = new javax.swing.JButton();
        jbtnexcluir1 = new javax.swing.JButton();
        jbtncancelar1 = new javax.swing.JButton();
        jbtnNext1 = new javax.swing.JButton();
        jbtnPrior1 = new javax.swing.JButton();
        jcbxproduto = new javax.swing.JComboBox();
        jtcodigo1 = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jtqtd = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();

        jLabel7.setText("jLabel7");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Master Pizza - Cadastro de Sabores");

        jLabel1.setText("Descrição");

        jbtnPesquisa.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/Pesquisa.png"))); // NOI18N
        jbtnPesquisa.setText("Pesquisar");
        jbtnPesquisa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnPesquisaActionPerformed(evt);
            }
        });

        jbtnFirst.setText("<<");
        jbtnFirst.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnFirstActionPerformed(evt);
            }
        });

        jbtnPrior.setText("<");
        jbtnPrior.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnPriorActionPerformed(evt);
            }
        });

        jbtnNext.setText(">");
        jbtnNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnNextActionPerformed(evt);
            }
        });

        jbtnLast.setText(">>");
        jbtnLast.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnLastActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jpNavegacaoLayout = new javax.swing.GroupLayout(jpNavegacao);
        jpNavegacao.setLayout(jpNavegacaoLayout);
        jpNavegacaoLayout.setHorizontalGroup(
            jpNavegacaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpNavegacaoLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jbtnFirst)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnPrior)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnNext)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnLast)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jpNavegacaoLayout.setVerticalGroup(
            jpNavegacaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jpNavegacaoLayout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addGroup(jpNavegacaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtnFirst)
                    .addComponent(jbtnPrior)
                    .addComponent(jbtnNext)
                    .addComponent(jbtnLast)))
        );

        javax.swing.GroupLayout jpnlPesqLayout = new javax.swing.GroupLayout(jpnlPesq);
        jpnlPesq.setLayout(jpnlPesqLayout);
        jpnlPesqLayout.setHorizontalGroup(
            jpnlPesqLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnlPesqLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jpnlPesqLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jpnlPesqLayout.createSequentialGroup()
                        .addComponent(jtPesq)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jbtnPesquisa))
                    .addGroup(jpnlPesqLayout.createSequentialGroup()
                        .addGap(54, 54, 54)
                        .addComponent(jpNavegacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
            .addComponent(jSeparator3, javax.swing.GroupLayout.Alignment.TRAILING)
        );
        jpnlPesqLayout.setVerticalGroup(
            jpnlPesqLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnlPesqLayout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(jpnlPesqLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jtPesq, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtnPesquisa))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jpNavegacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 5, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(1, 1, 1))
        );

        jbtnexcluir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/Excluir.png"))); // NOI18N
        jbtnexcluir.setText("Excluir");
        jbtnexcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnexcluirActionPerformed(evt);
            }
        });

        jbtnGravar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/Gravar.png"))); // NOI18N
        jbtnGravar.setText("Gravar");
        jbtnGravar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnGravarActionPerformed(evt);
            }
        });

        jbtncancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/Cancelar.png"))); // NOI18N
        jbtncancelar.setText("Cancelar");
        jbtncancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtncancelarActionPerformed(evt);
            }
        });

        jbtnNovo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/Incluir.png"))); // NOI18N
        jbtnNovo.setText("Novo");
        jbtnNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnNovoActionPerformed(evt);
            }
        });

        jbtnAlterar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/Alterar.png"))); // NOI18N
        jbtnAlterar.setText("Alterar");
        jbtnAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnAlterarActionPerformed(evt);
            }
        });

        jLabel2.setText("Código");

        jLabel3.setText("Sabor");

        jtDescricao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtDescricaoActionPerformed(evt);
            }
        });
        jtDescricao.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtDescricaoKeyPressed(evt);
            }
        });

        jPnlItens.setBorder(javax.swing.BorderFactory.createTitledBorder("Itens do Sabor"));

        labelProdutos.setText("Produtos");

        jbtnNovo1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/Incluir.png"))); // NOI18N
        jbtnNovo1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnNovo1ActionPerformed(evt);
            }
        });

        jbtnAlterar1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/Alterar.png"))); // NOI18N
        jbtnAlterar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnAlterar1ActionPerformed(evt);
            }
        });

        jbtnGravar1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/Gravar.png"))); // NOI18N
        jbtnGravar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnGravar1ActionPerformed(evt);
            }
        });

        jbtnexcluir1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/Excluir.png"))); // NOI18N
        jbtnexcluir1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnexcluir1ActionPerformed(evt);
            }
        });

        jbtncancelar1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/Cancelar.png"))); // NOI18N
        jbtncancelar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtncancelar1ActionPerformed(evt);
            }
        });

        jbtnNext1.setText(">");
        jbtnNext1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnNext1ActionPerformed(evt);
            }
        });

        jbtnPrior1.setText("<");
        jbtnPrior1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnPrior1ActionPerformed(evt);
            }
        });

        jLabel5.setText("Código");

        jtqtd.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jtqtdKeyTyped(evt);
            }
        });

        jLabel6.setText("Quantidade");

        javax.swing.GroupLayout jPnlItensLayout = new javax.swing.GroupLayout(jPnlItens);
        jPnlItens.setLayout(jPnlItensLayout);
        jPnlItensLayout.setHorizontalGroup(
            jPnlItensLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPnlItensLayout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(jPnlItensLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPnlItensLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jtcodigo1, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jbtnNovo1))
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPnlItensLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelProdutos)
                    .addGroup(jPnlItensLayout.createSequentialGroup()
                        .addComponent(jbtnAlterar1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jbtnGravar1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jbtnexcluir1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jbtncancelar1))
                    .addComponent(jcbxproduto, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPnlItensLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPnlItensLayout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addGap(79, 79, 79))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPnlItensLayout.createSequentialGroup()
                        .addGroup(jPnlItensLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jtqtd)
                            .addComponent(jbtnPrior1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtnNext1)
                        .addGap(18, 18, 18))))
        );
        jPnlItensLayout.setVerticalGroup(
            jPnlItensLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPnlItensLayout.createSequentialGroup()
                .addGroup(jPnlItensLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPnlItensLayout.createSequentialGroup()
                        .addGroup(jPnlItensLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(labelProdutos)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPnlItensLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jcbxproduto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jtcodigo1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPnlItensLayout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtqtd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(jPnlItensLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPnlItensLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jbtnNovo1)
                        .addComponent(jbtnexcluir1)
                        .addComponent(jbtnGravar1)
                        .addComponent(jbtnAlterar1)
                        .addComponent(jbtncancelar1))
                    .addGroup(jPnlItensLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jbtnPrior1)
                        .addComponent(jbtnNext1))))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jpnlPesq, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jbtnNovo)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jbtnAlterar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jbtnGravar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jbtnexcluir)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jbtncancelar))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(5, 5, 5)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jtcodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel2))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addComponent(jtDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, 273, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(0, 3, Short.MAX_VALUE))
                    .addComponent(jPnlItens, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
            .addComponent(jSeparator1)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jpnlPesq, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtcodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPnlItens, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtncancelar)
                    .addComponent(jbtnexcluir)
                    .addComponent(jbtnGravar)
                    .addComponent(jbtnAlterar)
                    .addComponent(jbtnNovo))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbtnPesquisaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnPesquisaActionPerformed
        sabores sab = new sabores();
        sab.setDescricao(jtPesq.getText());
        listSabores = sab.getItens();
        TravaBtns(false, true, true,false);
        jbtnFirstActionPerformed(evt);
    }//GEN-LAST:event_jbtnPesquisaActionPerformed

    private void jbtnFirstActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnFirstActionPerformed
        movPonteiro(true, false, false, false);
    }//GEN-LAST:event_jbtnFirstActionPerformed

    private void jbtnPriorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnPriorActionPerformed
        movPonteiro(false, true, false, false);
    }//GEN-LAST:event_jbtnPriorActionPerformed

    private void jbtnNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnNextActionPerformed
        movPonteiro(false, false, true, false);
    }//GEN-LAST:event_jbtnNextActionPerformed

    private void jbtnLastActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnLastActionPerformed
        movPonteiro(false, false, false, true);
    }//GEN-LAST:event_jbtnLastActionPerformed

    private void jbtnexcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnexcluirActionPerformed

        if (listSabores.size() <= 0) {
            JOptionPane.showMessageDialog(null, "Nenhum registro Encontrado");
        } else {
            if(!listSabores.get(ponteiro).ChecaMovimento()){
                JOptionPane.showMessageDialog(null,"..:IMPOSSÍVEL::.. Existem pizzas com esse sabor.Verifique!");
                return;
            }
            if (JOptionPane.showConfirmDialog(null, "Deseja exluir o Sabor: " + jtDescricao.getText()) == 0) {
                if (listSabores.get(ponteiro).excluir()) {
                    JOptionPane.showMessageDialog(null, "Excluido com Sucesso !");
                    limpaCampo();
                    jbtnPesquisaActionPerformed(evt);
                } else {
                    JOptionPane.showMessageDialog(null, "Erro ao Excluir Sabor");
                }
            }
        }
    }//GEN-LAST:event_jbtnexcluirActionPerformed

    private void jbtnGravarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnGravarActionPerformed
        if (Validacao(false)) {
            sabores sabor = new sabores();

            sabor.setDescricao(this.jtDescricao.getText());
            if (Incluindo) {
                if (sabor.incluir()) {
                    TravaBtns(false, true, false,false);

                    jtPesq.setText(sabor.getDescricao());
                    jbtnPesquisaActionPerformed(evt);
                    jtPesq.setText("");

                    JOptionPane.showMessageDialog(null, "Incluido com Sucesso !");

                } else {
                    JOptionPane.showMessageDialog(null, "Erro ao Incluir sabor");
                }
            } else {
                sabor.setId_sabores(Integer.parseInt(this.jtcodigo.getText()));
                if (sabor.alterar()) {

                    jtPesq.setText(sabor.getDescricao());
                    jbtnPesquisaActionPerformed(evt);
                    jtPesq.setText("");

                    JOptionPane.showMessageDialog(null, "Alterado com Sucesso !");
                } else {
                    JOptionPane.showMessageDialog(null, "Erro ao Alterar sabor");
                }
            }

        }
    }//GEN-LAST:event_jbtnGravarActionPerformed

    private void jbtncancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtncancelarActionPerformed
        Incluindo = false;
        TravaBtns(false, true, false,false);
        limpaCampo();
    }//GEN-LAST:event_jbtncancelarActionPerformed

    private void jbtnNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnNovoActionPerformed
        Incluindo = true;
        Incluindo_itens = !Incluindo;
        TravaBtns(true, false, false,false);
        limpaCampo();
    }//GEN-LAST:event_jbtnNovoActionPerformed

    private void jbtnAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnAlterarActionPerformed
        if (listSabores.size() > 0) {
            TravaBtns(true, false, false,false);
            Incluindo = false;
        } else {
            JOptionPane.showMessageDialog(null, "Nenhum registro Encontrado");
        }
    }//GEN-LAST:event_jbtnAlterarActionPerformed

    private void jtDescricaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtDescricaoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jtDescricaoActionPerformed

    private void jbtnNovo1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnNovo1ActionPerformed
        // TODO add your handling code here:
        if(listSabores.size() > 0){
            jtcodigo1.setText("");
            jtqtd.setText("");
            CarregaCombobox();
            Incluindo_itens = true;
            Incluindo = ! Incluindo_itens;
            TravaBtns(false, false, false,true);                    
        }else
            JOptionPane.showMessageDialog(rootPane,"Favor cadastre o Sabor primeiro!");

            
        
    }//GEN-LAST:event_jbtnNovo1ActionPerformed

    private void jbtnAlterar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnAlterar1ActionPerformed
        if(listSabores.size() > 0){
            Incluindo_itens = false;
            Incluindo = ! Incluindo_itens;
            TravaBtns(false, false, false,true);                    
            
            String  Strpro =(String) jcbxproduto.getSelectedItem();
            CarregaCombobox();
            
            //reposiciona o combobox no registro que vai ser alterado
             utlp.getIndexComboboxSet(jcbxproduto,Strpro);
            
        }else
            JOptionPane.showMessageDialog(rootPane,"Nehum registro encontrado!");
        // TODO add your handling code here:
        
    }//GEN-LAST:event_jbtnAlterar1ActionPerformed

    private void jbtnGravar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnGravar1ActionPerformed
        // TODO add your handling code here:
        if(Validacao(true)){
            sabores_Itens sabi = new sabores_Itens();
            produtos produto = new produtos();
            ArrayList<produtos> lstpr = new ArrayList<produtos>();

            produto.setDescricao((String) this.jcbxproduto.getSelectedItem());                 
            lstpr = produto.getItens();
            
            sabi.setId_sabores(Integer.parseInt(jtcodigo.getText()));
            sabi.setId_produtos(lstpr.get(0).getId_produtos());
            sabi.setQtd(Integer.parseInt(jtqtd.getText()));
            if(Incluindo_itens)
            {    
                if (sabi.incluir()) {
                    TravaBtns(false, true, false,false);

                    jtPesq.setText(jtDescricao.getText());
                    jbtnPesquisaActionPerformed(evt);

                    JOptionPane.showMessageDialog(null, "Incluido com Sucesso !");

                } else {
                   JOptionPane.showMessageDialog(null, "Erro ao Incluir item");
                }
            }
            else
            {
                sabi.setId_sabores_itens(Integer.parseInt(jtcodigo1.getText()));
                if(sabi.alterar()){
                    TravaBtns(false, true, false,false);

                    jtPesq.setText(jtDescricao.getText());
                    jbtnPesquisaActionPerformed(evt);

                    JOptionPane.showMessageDialog(null, "alterado com Sucesso !");

                } else {
                   JOptionPane.showMessageDialog(null, "Erro ao alterar item");
                }
                    
                    
            }
        }
        
    }//GEN-LAST:event_jbtnGravar1ActionPerformed

    private void jbtnexcluir1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnexcluir1ActionPerformed

        if (listSabores.size() <= 0 ||listSabores.get(ponteiro).getListsabores_itens().size() <=0 ) {
            JOptionPane.showMessageDialog(null, "Nenhum registro Encontrado");
        } else {
            if (JOptionPane.showConfirmDialog(null, "Deseja exluir o item : " +jcbxproduto.getSelectedItem()) == 0) {
                if (listSabores.get(ponteiro).getListsabores_itens().get(ponteiro_itens).excluir()) {
                    jtPesq.setText(jtDescricao.getText());
                    jbtnPesquisaActionPerformed(evt);
                    JOptionPane.showMessageDialog(null, "Excluido com Sucesso !");

                    //jbtnPrior1ActionPerformed(evt);
                    //jbtnPesquisaActionPerformed(evt);
                } else {
                    JOptionPane.showMessageDialog(null, "Erro ao Excluir Sabor");
                }
            }
        }

    }//GEN-LAST:event_jbtnexcluir1ActionPerformed

    private void jbtncancelar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtncancelar1ActionPerformed
        // TODO add your handling code here:
        if(Incluindo_itens)
            jbtnPesquisaActionPerformed(evt);
        else{            
            jbtnNext1ActionPerformed(evt);
            jbtnPrior1ActionPerformed(evt);
        }        
        TravaBtns(false, true, false, false);
    }//GEN-LAST:event_jbtncancelar1ActionPerformed

    private void jbtnNext1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnNext1ActionPerformed
        movponteiroItens(false, true);
    }//GEN-LAST:event_jbtnNext1ActionPerformed
    private void movponteiroItens(boolean prior,boolean next){
        if(listSabores.size() > 0 && listSabores.get(ponteiro).getListsabores_itens().size()> 0)
        {        
            
            if(prior){
                ponteiro_itens = ponteiro_itens -1;                
                if(ponteiro_itens > listSabores.get(ponteiro).getListsabores_itens().size())
                    ponteiro_itens = listSabores.get(ponteiro).getListsabores_itens().size() - 1;
            }        
            else
            if(next)    
            {
                ponteiro_itens = ponteiro_itens +1;                
                if(ponteiro_itens > (listSabores.get(ponteiro).getListsabores_itens().size() -1))                
                    ponteiro_itens = listSabores.get(ponteiro).getListsabores_itens().size() - 1;
            }
            
            if(ponteiro_itens < 0) ponteiro_itens = 0;
            
            jtcodigo1.setText(Integer.toString(this.listSabores.get(ponteiro).getListsabores_itens().get(ponteiro_itens).getId_sabores_itens()));            
            jtqtd.setText(Integer.toString(this.listSabores.get(ponteiro).getListsabores_itens().get(ponteiro_itens).getQtd()));
            String S = this.listSabores.get(ponteiro).getListsabores_itens().get(ponteiro_itens).getDesc_produto();
            jcbxproduto.removeAllItems();
            jcbxproduto.addItem(S);
            jcbxproduto.setSelectedIndex(0);                        
        }else
        {
            jtcodigo1.setText("");
            jtqtd.setText("");
        }   
            
        
    }
    private void jbtnPrior1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnPrior1ActionPerformed
        movponteiroItens(true, false);
    }//GEN-LAST:event_jbtnPrior1ActionPerformed

    private void jtqtdKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtqtdKeyTyped
        String caracteres = "0987654321";
        if (!caracteres.contains(evt.getKeyChar() + "")) {
            evt.consume();
        }
    }//GEN-LAST:event_jtqtdKeyTyped

    private void jtDescricaoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtDescricaoKeyPressed
        // TODO add your handling code here:
        utlp.MaxCaracter(jtDescricao,35);
    }//GEN-LAST:event_jtDescricaoKeyPressed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FormSabores.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FormSabores.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FormSabores.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FormSabores.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FormSabores().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPnlItens;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JButton jbtnAlterar;
    private javax.swing.JButton jbtnAlterar1;
    private javax.swing.JButton jbtnFirst;
    private javax.swing.JButton jbtnGravar;
    private javax.swing.JButton jbtnGravar1;
    private javax.swing.JButton jbtnLast;
    private javax.swing.JButton jbtnNext;
    private javax.swing.JButton jbtnNext1;
    private javax.swing.JButton jbtnNovo;
    private javax.swing.JButton jbtnNovo1;
    private javax.swing.JButton jbtnPesquisa;
    private javax.swing.JButton jbtnPrior;
    private javax.swing.JButton jbtnPrior1;
    private javax.swing.JButton jbtncancelar;
    private javax.swing.JButton jbtncancelar1;
    private javax.swing.JButton jbtnexcluir;
    private javax.swing.JButton jbtnexcluir1;
    private javax.swing.JComboBox jcbxproduto;
    private javax.swing.JPanel jpNavegacao;
    private javax.swing.JPanel jpnlPesq;
    private javax.swing.JTextField jtDescricao;
    private javax.swing.JTextField jtPesq;
    private javax.swing.JTextField jtcodigo;
    private javax.swing.JTextField jtcodigo1;
    private javax.swing.JTextField jtqtd;
    private javax.swing.JLabel labelProdutos;
    // End of variables declaration//GEN-END:variables
}
